//https://leetcode.cn/problems/happy-number/description/

class Solution {
public:
    int sum(int n) {
        int sum = 0;
        while (n) {
            int t = n % 10;
            sum += t * t;
            n /= 10;
        }
        return sum;
    }


    bool isHappy(int n) {
        int slow = n; int fast = sum(n);
        while (slow != fast) {
            slow = sum(slow);
            fast = sum(sum(fast));
        }
        return slow == 1;
    }
};